package com.example.capturetheflag.helper;

import org.json.JSONArray;
import android.os.Handler;
import android.view.View;
import com.example.capturetheflag.R;
import com.example.capturetheflag.connection.HttpGetRequest;

public class ThreeSecondRequest implements Runnable {

	public Handler handler;
	public Object t;
	public SitesOverlay itemizedOverlay;
	public View view;
	
	public ThreeSecondRequest(Handler h,SitesOverlay itemizedOverlay, View view) 
	{
		// TODO Auto-generated constructor stub
		this.handler = h;
		this.itemizedOverlay = itemizedOverlay;
		this.view = view;
	}
	
	public void run() 
	{
		// TODO Auto-generated method stub
		/* do what you need to do */
		//Toast.makeText((Context) t, "Server connection...", Toast.LENGTH_SHORT).show();
		boolean nobodyHasFlag_A = true;
		boolean nobodyHasFlag_B = true;
		
		try
		{	
			String playerID = Singleton.getInstance().playerID;
			String gameID = Singleton.getInstance().gameID;
			String latitude = Integer.toString(Singleton.getInstance().latitude);
			String longitude = Integer.toString(Singleton.getInstance().longitude);
			
			HttpGetRequest httpGetRequest = new HttpGetRequest();
			httpGetRequest.execute("http://proinz.studentlive.pl/GeoService.svc/GetPlayerPoints/"+playerID+"/"+gameID+"/"+latitude+"/"+longitude);
			JSONArray itemArray = Singleton.getInstance().playersPossition;
			itemizedOverlay.clear();
			
			for (int i = 0; i < itemArray.length(); i++) 
			{
				int x = Integer.decode(itemArray.getJSONObject(i).getString("latitude").toString());
				int x1 = Integer.parseInt((Singleton.getInstance().playerID));
				if(x == x1)
				{
					Singleton.getInstance().flag = Boolean.parseBoolean(itemArray.getJSONObject(i).getString("flag").toString());
					Singleton.getInstance().points = Integer.decode(itemArray.getJSONObject(i).getString("longitude").toString());
					//continue;
				}
				else
				{
					int y = Integer.decode(itemArray.getJSONObject(i).getString("longitude").toString());
					boolean team = Boolean.parseBoolean(itemArray.getJSONObject(i).getString("team").toString());
					boolean flag = Boolean.parseBoolean(itemArray.getJSONObject(i).getString("flag").toString());
					if(team)
					{
						if(flag)
						{
							itemizedOverlay.add(x,y, Integer.toString(i), R.drawable.a_with_flag, view);
							nobodyHasFlag_A = false;
						}
						else
							itemizedOverlay.add(x,y, Integer.toString(i), R.drawable.a, view);
					}
					else
					{
						if(flag)
						{
							itemizedOverlay.add(x,y, Integer.toString(i), R.drawable.b_with_flag, view);
							nobodyHasFlag_B = false;
						}
						else
						itemizedOverlay.add(x,y, Integer.toString(i), R.drawable.b, view);
					}
				}
			}
			
			boolean IhaveflagA = false;
			boolean IhaveflagB = false;
			if(Singleton.getInstance().flag)
				if(Singleton.getInstance().team)
					IhaveflagA = true;
				else
					IhaveflagB = true;

			
			
			if(nobodyHasFlag_A && !IhaveflagA){
				int x = Singleton.getInstance().a.getLatitudeE6();
				int y = Singleton.getInstance().a.getLongitudeE6();
				itemizedOverlay.add(x,y, "a", R.drawable.flag_a, view);
			}
			if(nobodyHasFlag_B && !IhaveflagB){
				int x = Singleton.getInstance().c.getLatitudeE6();
				int y = Singleton.getInstance().c.getLongitudeE6();
				itemizedOverlay.add(x,y, "c", R.drawable.flag_b, view);
			}
			
		}catch(Exception e){
		}
		
	      /* and here comes the "trick" */
	      handler.postDelayed(this, 3000);
	}
}